const api = require('../../utils/api.js');
const app = getApp();

Page({
  data: {
    useTabbar: !(app.tabbarindex === 0 && app.siteInfo.tabbar_type === '1'),
    tabs: ['商品', '专题'],
    tabCurrent: 0,
    list: [],
    page: 1,
    canPullUp: true, // 是否还有更多数据
    abnorData: {
      imgSrc: app.imgUrl + 'abnor-20.png',
      text: '暂无收藏，快去逛逛吧',
    },
    goodsCount: 0,
    topicCount: 0,
    isLoad: false,
  },

  onLoad() {
    app.loadCurrencyUnit().then(res => {
      this.setData({
        unit: res.unit,
      });
    });
  },

  collectFormId(e) {
    app.collectFormId(e);
  },

  onNavigator(e) {
    let { gid, gtype } = e.currentTarget.dataset;
    if (Number(gtype) === 0) {
      app.util.navigateTo('/ws_micro_shop/pages/goods/goods', { id: gid });
    } else if (Number(gtype) === 2) {
      app.util.navigateTo('ws_micro_shop_ms/pages/miaosha/details/details', {
        gid: gid,
      });
    }
  },

  getList() {
    let { tabCurrent, page, list: d_list } = this.data;
    const url = tabCurrent === 0 ? api.user.favorite_list : api.user.topic_favorite_list;
    wx.showLoading({
      title: '正在加载',
      mask: true,
    });
    app.ws_micro_shoprequest({
      url,
      data: {
        page,
      },
      success: res => {
        if (res && res.code === 0 && res.data && res.data.list) {
          let list = res.data.list;
          if (page > 1) {
            list = [...d_list, ...list];
          }
          // 新加接口字段 row_count 当前收藏数  other_count 是另一个  cd 20200111
          this.setData({
            list,
            isLoad: true,
            canPullUp: res.data.list.length <= 0 ? false : true,
            goodsCount: tabCurrent === 0 ? res.data.row_count : res.data.other_count,
            topicCount: tabCurrent === 1 ? res.data.row_count : res.data.other_count,
          });
        }
        
      },
      complete: function(e) {
        wx.hideLoading();
      },
    });
  },

  onTab(e) {
    const { index: tabCurrent } = e.currentTarget.dataset;
    this.setData(
      {
        tabCurrent,
        page: 1,
        canPullUp: true,
        isLoad: false,
      },
      () => {
        this.getList();
      }
    );
  },

  onNavigate(e) {
    const { tabCurrent } = this.data;
    const {
      item: { id = '', goods_id = '', type = '' },
    } = e.currentTarget.dataset;

    let url;
    let params;

    if (tabCurrent === 0) {
      if (Number(type) === 0) {
        url = '/ws_micro_shop/pages/goods/goods';
        params = { id: goods_id };
      } else {
        url = '/ws_micro_shop_ms/pages/miaosha/details/details';
        params = { gid: goods_id };
      }
    } else {
      url = 'ws_micro_shop/pages/topic/topic';
      params = { id };
    }

    app.util.navigateTo(url, params);
  },

  onReachBottom() {
    let { page, canPullUp } = this.data;

    if (canPullUp) {
      this.setData({
        page: ++page,
      });
      this.getList();
    }
  },

  onShow() {
    this.setData(
      {
        page: 1,
        list: [],
        canPullUp: true,
        isLoad: false,
      },
      () => {
        this.getList();
      }
    );
  },
});
